package com.huoyun.company.action;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.huoyun.base.action.BaseAction;
import com.huoyun.company.bean.CompanyInfo;
import com.huoyun.company.bean.CompanyPerson;
import com.huoyun.company.service.CompanyService;
import com.huoyun.cargo.service.CargoService;
import com.huoyun.util.DateUtil;
import com.huoyun.util.FileUtil;
import com.huoyun.util.StringUtil;

public class CompanyAction extends BaseAction {
	private CompanyService companyService;
	private CompanyInfo company;
	private CompanyPerson companyPerson;

	private File logoImgFile;
	private String logoImgFileFileName;

	public String preCompany() {
		long userId = this.getLoginUser().getId();
		this.company = companyService.getCompanyInfoByUserId(userId);
		if (company != null)
			companyPerson = companyService.getCompanyPersonByCompanyId(company
					.getId());
		return SUCCESS;
	}

	/**
	 * 进入修改商标页面
	 * @author yangyang
	 * @return
	 */
	public String preChangeLogo() {
		try {
			long userId = this.getLoginUser().getId();
			this.company = companyService.getCompanyInfoByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 修改商标
	 * @author yangyang
	 * @return
	 */
	public String changeLogo() {
		long userId = this.getLoginUser().getId();
		
		if (logoImgFile != null) {
			String path = FileUtil.UPLOAD_LOGO_DIR + "/"
					+ DateUtil.getCurrentDate();
			String saveFolderPath = ServletActionContext.getRequest()
					.getRealPath(path);

			File saveFolder = new File(saveFolderPath);

			if (!saveFolder.exists()) {
				saveFolder.mkdir();
				System.out.println(saveFolder.getAbsolutePath());
			}

			File logoImgFile_new = new File(saveFolderPath, UUID.randomUUID()
					.toString()
					+ "." + StringUtil.getLastName(logoImgFileFileName));

			try {
				FileUtil.copyFile(logoImgFile, logoImgFile_new);
			} catch (IOException e) {
				this.setResultMsg("上传文件失败！");
			}
			try {
				company=companyService.getCompanyInfoByUserId(userId);
				company.setLogoImg(path + "/" + logoImgFile_new.getName());
			} catch (Exception e) {
				e.printStackTrace();
				return INPUT;
			}
		}
		try {
			
			companyService.updateCompanyInfo(company);
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}

	public String editCompany() {
		long userId = this.getLoginUser().getId();

		if (logoImgFile != null) {
			String path = FileUtil.UPLOAD_LOGO_DIR + "/"
					+ DateUtil.getCurrentDate();
			String saveFolderPath = ServletActionContext.getRequest()
					.getRealPath(path);

			File saveFolder = new File(saveFolderPath);

			if (!saveFolder.exists()) {
				saveFolder.mkdir();
				System.out.println(saveFolder.getAbsolutePath());
			}

			File logoImgFile_new = new File(saveFolderPath, UUID.randomUUID()
					.toString()
					+ "." + StringUtil.getLastName(logoImgFileFileName));

			try {
				FileUtil.copyFile(logoImgFile, logoImgFile_new);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				this.setResultMsg("上传文件失败！");
			}
			company.setLogoImg(path + "/" + logoImgFile_new.getName());
		}

		CompanyInfo queryedCompany = companyService
				.getCompanyInfoByUserId(userId);
		if (queryedCompany == null) {

			company.setUserId(this.getLoginUser().getId());

			companyService.saveCompanyInfo(company);
			companyPerson.setCompanyId(company.getId());

			companyService.saveCompanyPerson(companyPerson);
			this.resultMsg = "保存公司信息成功！";
			return "save";
		} else {

			companyService.updateCompanyInfo(company);
			// System.out.println("company")
			CompanyPerson queryedCompanyPerson = companyService
					.getCompanyPersonByCompanyId(queryedCompany.getId());
			if (queryedCompanyPerson == null) {
				companyPerson.setCompanyId(queryedCompany.getId());
				companyService.saveCompanyPerson(companyPerson);
			} else {
				companyService.updateCompanyPerson(companyPerson);
			}
			this.resultMsg = "更新公司信息成功！";
			return "modify";
		}

	}

	public String regCompanyCargo() {
		try {
			System.out.println("company=" + company + " companyPerson="
					+ companyPerson);
			companyService.saveCompanyInfo(company);
			companyService.saveCompanyPerson(companyPerson);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String ModifyCompanyCargo() {
		try {
			companyService.updateCompanyInfo(company);
			companyService.updateCompanyPerson(companyPerson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String editCompanyInfo() {

		try {
			company.setUserId(this.getLoginUser().getId());

			companyService.updateCompanyInfo(company);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String editCompanyLegalPerson() {
		try {
			companyService.updateCompanyInfo(company);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String editCompanyContactPerson() {
		try {
			companyService.updateCompanyPerson(companyPerson);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public CompanyInfo getCompany() {
		return company;
	}

	public void setCompany(CompanyInfo company) {
		this.company = company;
	}

	public CompanyPerson getCompanyPerson() {
		return companyPerson;
	}

	public void setCompanyPerson(CompanyPerson companyPerson) {
		this.companyPerson = companyPerson;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public File getLogoImgFile() {
		return logoImgFile;
	}

	public void setLogoImgFile(File logoImgFile) {
		this.logoImgFile = logoImgFile;
	}

	public String getLogoImgFileFileName() {
		return logoImgFileFileName;
	}

	public void setLogoImgFileFileName(String logoImgFileFileName) {
		this.logoImgFileFileName = logoImgFileFileName;
	}

}
